<?php
$SITEROOT = '../';
include_once($SITEROOT . 'assets/snippets/shoppingCart/dbconfig.inc.php');
include_once($SITEROOT . 'assets/snippets/shoppingCart/kiwee.class.php');

$kiwee = new Kiwee();

if(IN_MANAGER_MODE!="true") die('<b>' . $_lang['kiwee_include_order_error'] . '</b>');
if ($_SESSION['mgrPermissions']['settings'] != "1") { echo 'Insufficient permissions for this module.'; exit; }

$displayStyle = ( ($_SESSION['browser']=='mz') || ($_SESSION['browser']=='op') ) ? "table-row" : "block" ;

?>

<script type="text/javascript">
function checkIM() {
	im_on = document.settings.im_plugin[0].checked; // check if im_plugin is on
	if(im_on==true) {
		showHide(/imRow/, 1);
	}
};

function checkCustomIcons() {
	if(document.settings.editor_toolbar.selectedIndex!=3) {
		showHide(/custom/,0);
	}
};

function showHide(what, onoff){

	var all = document.getElementsByTagName( "*" );
	var l = all.length;
	var buttonRe = what;
	var id, el, stylevar;

	if(onoff==1) {
		stylevar = "<?php echo $displayStyle; ?>";
	} else {
		stylevar = "none";
	}

	for ( var i = 0; i < l; i++ ) {
		el = all[i]
		id = el.id;
		if ( id == "" ) continue;
		if (buttonRe.test(id)) {
			el.style.display = stylevar;
		}
	}
};

function addContentType(){
	var i,o,exists=false;
	var txt = document.settings.txt_custom_contenttype;
	var lst = document.settings.lst_custom_contenttype;
	for(i=0;i<lst.options.length;i++)
	{
		if(lst.options[i].value==txt.value) {
			exists=true;
			break;
		}
	}
	if (!exists) {
		o = new Option(txt.value,txt.value);
		lst.options[lst.options.length]= o;
		updateContentType();
	}
	txt.value='';
}
function removeContentType(){
	var i;
	var lst = document.settings.lst_custom_contenttype;
	for(i=0;i<lst.options.length;i++) {
		if(lst.options[i].selected) {
			lst.remove(i);
			break;
		}
	}
	updateContentType();
}
function updateContentType(){
	var i,o,ol=[];
	var lst = document.settings.lst_custom_contenttype;
	var ct = document.settings.custom_contenttype;
	while(lst.options.length) {
		ol[ol.length] = lst.options[0].value;
		lst.options[0]= null;
	}
	if(ol.sort) ol.sort();
	ct.value = ol.join(",");
	for(i=0;i<ol.length;i++) {
		o = new Option(ol[i],ol[i]);
		lst.options[lst.options.length]= o;
	}
	documentDirty = true;
}

	function ajaxrendercomponent(name) {
		alert(name);
	}
	</script>
<link href="actions/includes/kiwee.css" rel="stylesheet" type="text/css" />
<div class="kiwee-msc" id="mascot"><a href="http://www.kiweecommerce.org"><img src="actions/includes/Kiwee-small.gif" /></a></div>
<div class="subTitle">
	<span class="right"><?php echo $_lang['settings_title']; ?></span>

	<table cellpadding="0" cellspacing="0" class="actionButtons">
		<tr>
			<td id="Button1"><a href="#" onclick="documentDirty=false; document.settings.submit();"><img src="media/style/<?php echo $manager_theme ? "$manager_theme/":""; ?>images/icons/save.gif" /> <?php echo $_lang['save']; ?></a></td>
			<td id="Button5"><a href="#" onclick="documentDirty=false;document.location.href='index.php?a=2';"><img src="media/style/<?php echo $manager_theme ? "$manager_theme/":""; ?>images/icons/cancel.gif" /> <?php echo $_lang['cancel']; ?></a></td>
		</tr>
	</table>
</div>
<div class="sectionHeader"><?php echo $_lang["kconfig_header"]; ?></div>
<form name="settings" action="index.php?a=490" method="post" />
<div style="margin: 0 10px 0 20px">
     <!-- this field is used to check site settings have been entered/ updated after install or upgrade -->
    <?php if(!isset($settings_version) || $settings_version!=$version) { ?>
    <div class='sectionBody'><?php echo $_lang['settings_after_install']; ?></div>
    <?php } ?>
    <script type="text/javascript" src="media/script/tabpane.js"></script>
    <div class="tab-pane" id="settingsPane">
      <script type="text/javascript">
		tpSettings = new WebFXTabPane( document.getElementById( "settingsPane" ) );
	</script>

<?php	
include_once('../assets/snippets/shoppingCart/kiwee.class.php');
$kiwee = new Kiwee();

// Get All Options
$result = $kiwee->db->sel($kiwee->config['fpx'], 'config ORDER BY category,subcategory', 'category, subcategory, optname, optvalues, renderas, shortdesc, longdesc');
$opts = $result;
// Get Values for Options
$result = $kiwee->db->sel($kiwee->config['fpx'], 'config_values', 'optname,optval');

for ($k=0; $k < sizeof($result); $k++){
	$row = $result[$k];
	$optvalues[$row['optname']] = $row['optval'];
}

$first='true';
$prevOption='';
$currOption='';
if (sizeof($opts) > 1) {
	for ($i=0; $i< sizeof($opts); $i++) {
		$row = $opts[$i];
		$prevOption = $currOption;
		$currOption['category'] = $row['category'];
		$currOption['subcategory'] = $row['subcategory'];
		$currOption['name'] = $row['optname'];
		$currOption['values'] = $row['optvalues'];
		$currOption['renderas'] = $row['renderas'];
		$currOption['shortdesc'] = $row['shortdesc'];
		$currOption['longdesc'] = $row['longdesc'];
		if ($prevOption['category'] != $currOption['category']){
			if ($i != 0){
				echo "</table>";
	      		echo '</div>';					
			}
		    echo '<div class="tab-page" id="tabpage-' . $currOption['name'] . '">' . "\n";
	        echo '	<h2 class="tab">' . $currOption['category'] . '</h2>' . "\n";
    	    echo '	<script type="text/javascript">tpSettings.addTabPage( document.getElementById( "tabpage-' . $currOption['name'] . '" ) );</script>' . "\n";			
    	    echo '<table border="0" cellspacing="0" cellpadding="3">' . "\n";
		}
		
		if ($prevOption['subcategory'] != $currOption['subcategory']){
			echo '<tr>' . "\n";
			echo '<td colspan="2">' . "\n";
			echo '<h2>' . $currOption['subcategory'] . '</h2>' . "\n";		
			echo '</td>' . "\n";
			echo '</tr>' . "\n";
		}

		echo ' <tr>' . "\n";
		echo '   <td nowrap class="warning"><b>' . $currOption['shortdesc'] . '</b></td>' . "\n";
		echo '<td >' . "\n";
		
		// This is to check for the default option
		$sel='';

		if ($currOption['renderas']=='TEXTBOX'){
			echo '   <input onchange="documentDirty=true;" type="text" maxlength="255" style="width: 200px;" name="' . $currOption['name'] . '" id="' . $currOption['name'] . '" value="' . $optvalues[$currOption['name']] . '" />' . "\n";
		} else if ($currOption['renderas']=='TEXTAREA'){
			echo '<textarea onchange="documentDirty=true;" id="' . $currOption['name'] . '" name="' . $currOption['name'] . '" rows="10" cols="12" >' . "\n";
            echo '</textarea>';
		} else if ($currOption['renderas']=='QUERYPICKER'){
			// TODO: Chad here
			switch ($currOption['values']) {
				case 'template':
					$result = $kiwee->db->sel($kiwee->config['pfx'], 'site_templates ORDER BY category,id', 'id, templatename, category');
					echo '<select onchange="documentDirty=true;" id="' . $currOption['name'] . '" name="' . $currOption['name'] . '" >' . "\n";
		            for ($j=0; $j < sizeof($result); $j++) {
						$row = $result[$j];
		            	if ($row['id'] == $optvalues[$currOption['name']]) {$sel="selected";}
		               		echo '<option ' . $sel . ' value="' . $row['id'] . '">' . $row['templatename'] . "</option>\n";
		            		$sel="";
		          	}
					echo '</select>' . "\n";
					break;
				case 'chunk':
					$result = $kiwee->db->sel($kiwee->config['pfx'], 'site_htmlsnippets ORDER BY name,id', 'id, name');
					echo '<select onchange="documentDirty=true;" id="' . $currOption['name'] . '" name="' . $currOption['name'] . '" >' . "\n";
		            for ($j=0; $j < sizeof($result); $j++) {
						$row = $result[$j];
		            	if ($row['id'] == $optvalues[$currOption['name']]) {$sel="selected";}
		               		echo '<option ' . $sel . ' value="' . $row['id'] . '">' . $row['name'] . "</option>\n";
		            		$sel="";
		          	}
					echo '</select>' . "\n";
					break;
				case 'page':
					$result = $kiwee->db->sel($kiwee->config['pfx'], 'site_content ORDER BY pagetitle,id', 'id, pagetitle');
					echo '<select onchange="documentDirty=true;" id="' . $currOption['name'] . '" name="' . $currOption['name'] . '" >' . "\n";
		            for ($j=0; $j < sizeof($result); $j++) {
						$row = $result[$j];
		            	if ($row['id'] == $optvalues[$currOption['name']]) {$sel="selected";}
		               		echo '<option ' . $sel . ' value="' . $row['id'] . '">' . $row['pagetitle'] . "</option>\n";
		            		$sel="";
		          	}
					echo '</select>' . "\n";
					break;
				case 'category':
					$result = $kiwee->db->sel($kiwee->config['pfx'], 'categories ORDER BY category,id', 'id, category');
					echo '<select onchange="documentDirty=true;" id="' . $currOption['name'] . '" name="' . $currOption['name'] . '" >' . "\n";
		            for ($j=0; $j < sizeof($result); $j++) {
						$row = $result[$j];
		            	if ($row['id'] == $optvalues[$currOption['name']]) {$sel="selected";}
		               		echo '<option ' . $sel . ' value="' . $row['id'] . '">' . $row['category'] . "</option>\n";
		            		$sel="";
		          	}
					echo '</select>' . "\n";
					break;
			}
			
		} else if ($currOption['renderas']=='DROPDOWN'){
			echo '<select onchange="documentDirty=true;" id="' . $currOption['name'] . '" name="' . $currOption['name'] . '" >' . "\n";
			
			$allopts = explode("," , $currOption['values']);
            for($j=0; $j < sizeof($allopts); $j++) {
            	if ($optvalues[$currOption['name']]==$allopts[$j]) {$sel="selected";}
               		echo '<option ' . $sel . ' value="' . $allopts[$j] . '">' . "\n";
					echo $allopts[$j];
					echo '</option>' . "\n";
            		$sel="";
          	}
			echo '</select>' . "\n";
		} else if ($currOption['renderas']=='RADIOBUTTON'){
			$allopts = explode("," , $currOption['values']);
			for ($l=0; $l < sizeof($allopts); $l++){
				if ($optvalues[$currOption['name']]==$allopts[$l]) {$sel="CHECKED";}
				echo '<input ' . $sel . ' type="radio" onchange="documentDirty=true;" id="' . $currOption['name'] . '-' . $l . '" value="' . $allopts[$l] . '" name="' . $currOption['name'] . '" > ' . $allopts[$l] . '<br />' . "\n";
				$sel='';
			}
		} 

		echo '</td>' . "\n";
		echo '</tr>' . "\n";

        echo '<tr>' . "\n";
        echo '<td>&nbsp;</td>' . "\n";
        echo '<td class="comment">' . $currOption['longdesc'] . '</td>' . "\n";
        echo '</tr>' . "\n";
        echo '<tr>' . "\n";
        echo '<td colspan="2"><div class="split"></div></td>' . "\n";
        echo '</tr>' . "\n";						
	}
}
?>
	<!-- Site Settings -->
    </div>
</div>
</form>
